module.exports = function($scope, $state, findExceptionList, list,  getDate, $resource, toaster, 
    $modal, ticketlist, createBackOrder, resend, getRedCorridorOrderList,
    getRedCorridorResentMsg, getRedCorridorTrSendSms, orderbacklist, relay,
    getOrderSimInfo, agencyOrderRepeatECode, updateTicketEffectTime, str2date,
    getroyalocOrdersState, testCreateBackOrder,categorylist, date2str, findByorderCartCode){
   
    $scope.tstate = [
        {'label':'--全部--', 'value':''},
        {'label':'未出票', 'value':'0'}, 
        {'label':'等待出票', 'value':'1'},
        {'label':'出票完成', 'value':'2'},
        {'label':'无需出票', 'value':'8'},
        {'label':'出票失败', 'value':'9'}
    ];
    $scope.ptype = [
      {'label':'--全部--', 'value':''},
      {'label':'其他支付、支付宝微信等', 'value':'0'}, 
      {'label':'现金支付', 'value':'1'},
      {'label':'景区批量出票（相当于现金直接出票）', 'value':'2'},
      {'label':'平台余额支付（balance）', 'value':'5'}, 
      {'label':'预存支付（credit）', 'value':'6'},
    ];
    $scope.pstate = [
      {'label':'--全部--', 'value':''},
      {'label':'未支付', 'value':'0'}, 
      {'label':'支付完成(无需支付)', 'value':'1'},
    ];
    $scope.pstype = [
      {'label':'--全部--', 'value':''},
      {'label':'支付宝', 'value':'1'}, 
      {'label':'微信', 'value':'2'},
      {'label':'现金支付', 'value':'3'},
      {'label':'景区批量出票（相当于现金直接出票）', 'value':'4'}, 
      {'label':'平台余额支付（balance）', 'value':'5'},
      {'label':'预存支付（credit）', 'value':'6'}, 
      {'label':'龙支付', 'value':'7'},
    ];
    $scope.ostate = [
      {'label':'--全部--', 'value':''},
      {'label':'正常', 'value':'0'}, 
      {'label':'取消', 'value':'1'},
    ];
    $scope.usestate = [
        {'label':'-全部-', 'value':null},
        {'label':'未使用', 'value':'0'},
        {'label':'已使用', 'value':'1'},
    ];

    $scope.searchform = {'moreSearch': false};

    $scope.date = {
        'lable': null,
        'opened': false
    }
    $scope.date1 = {
        'lable': null,
        'opened': false
    }
    $scope.date2 = {
      'lable': null,
      'opened': false
    }
    $scope.date3 = {
        'lable': null,
        'opened': false
    }
    $scope.tour_date = {
        'lable': null,
        'opened': false
    }

    $scope.dateOpen1 = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };

    $scope.loadPlaceList = function(){
        $resource('/api/as/tc/place/simpleList', {}, {}).save({}, function (res) {
            if (res.errcode === 0) {
                $scope.place_list = res.data;
                $scope.place_list.unshift({'name': '--全部景区--', 'code': ''});
            }
        });
    }
    $scope.loadPlaceList();

    /* 分页 * ========================================= */
    $scope.maxSize = 5;            //最多显示多少个按钮
    $scope.bigCurrentPage = 1;      //当前页码
    $scope.itemsPerPage = 10;         //每页显示几条
    $scope.search = {
        'isselect' : {
            'sale_category_name' : ''
        }
    }
    $scope.getcategorylist = function(){
        categorylist.save({'type':'sale_category'},function(res){
            if(res.errcode != 0){
                alert(res.errmsg);
                return;
            }
            $scope.category_type = res.data;
            $scope.category_type.unshift({ label: '--全部查询--', value: '' });
        })
    }
    $scope.getcategorylist();
    $scope.change = function(code){
        $scope.searchform.sale_category = code;
    }
    
    $scope.moreSearch = function(){
        $scope.searchform.moreSearch = !$scope.searchform.moreSearch;
    }
    
    $scope.load = function () {
        if($scope.date.lable && !$scope.date1.lable){
            toaster.warning({title: '', body: '开始和结束日期必须同时输入进行搜索'});
            return;
        }
        if($scope.date1.lable && !$scope.date.lable){
            toaster.warning({title: '', body: '开始和结束日期必须同时输入进行搜索'});
            return;
        }
        if($scope.date.lable && $scope.date1.lable && $scope.date.lable > $scope.date1.lable){
            toaster.warning({title: '', body: '开始日期不能大于结束日期'});
            return;
        }

        var para = {
            'pageNo': $scope.bigCurrentPage,
            'pageSize': $scope.itemsPerPage,
            'payTimeStart': getDate($scope.date.lable),
            'payTimeEnd': getDate($scope.date1.lable),
            'createTimeStart': getDate($scope.date2.lable),
            'createTimeEnd': getDate($scope.date3.lable),
            'code': $scope.searchform.code,
            'payState': $scope.searchform.payState,
            'payType': $scope.searchform.payType,
            'payFeeType': $scope.searchform.payFeeType,
            'cancelState': $scope.searchform.cancelState,
            'createBy': $scope.searchform.createBy,
            'deviceCode': $scope.searchform.deviceCode
        }
        if(para.start_time){
            para.start_time += " 00:00:00";
            para.end_time += " 23:59:59";
        }
        if($scope.searchform.person){
            if($scope.searchform.person.length == 18){
                para.cardno = $scope.searchform.person;
            } else if($scope.searchform.person.length == 11){
                para.mobile = $scope.searchform.person;
            } else {
                para.name = $scope.searchform.person;
            }
        }
        if($scope.searchform.moreSearch){
            para.ticket_state = $scope.searchform.ticket_state;
            para.sale_category = $scope.searchform.sale_category;
            para.place_code = $scope.searchform.place_code;
            para.company_code = $scope.searchform.company_code;
            para.keyword = $scope.searchform.keyword;
            para.sale_code = $scope.searchform.sale_code;
            para.use_state = $scope.searchform.use_state;
            para.tour_date = getDate($scope.tour_date.lable);
        }
        
        findExceptionList.save(para, function(res){
            if(res.errcode === 0) {
                $scope.objs = res.data.results ;
                $scope.bigTotalItems = res.data.totalRecord;
            } else {
                alert(res.errmsg);
            }
        });
    };
    $scope.load();


    $scope.ticketlist = function(obj){

        var ticketlist1 = ticketlist;
        var createBackOrder1 = createBackOrder;
        var getRedCorridorOrderList1 = getRedCorridorOrderList;
        var orderbacklist1 = orderbacklist;
        var getOrderSimInfo1 = getOrderSimInfo;
        var updateTicketEffectTime1 = updateTicketEffectTime;
        var getDate1 = getDate;
        var str2date1 = str2date;
        var getroyalocOrdersState1 = getroyalocOrdersState;
        
        //$state.go('app.orderticketlist', {'code' : code});

        var modalInstance = $modal.open({
          template: require('../views/orderticketlist.html'),
          controller: 'orderticketlist',
          size: 'lg',
          resolve: {
            'obj' : function(){
                return obj;
            },
            'ticketlist' : function(){
                return ticketlist1;
            },
            'createBackOrder' : function(){
                return createBackOrder1;
            },
            //红海滩
            'getRedCorridorOrderList' : function(){
                return getRedCorridorOrderList1;
            },
            //退票历史
            'orderbacklist' : function(){
                return orderbacklist1;
            },
            //北京票联  红海滩廊道
            'getOrderSimInfo' : function(){
                return getOrderSimInfo1;
            },
            //修改生效时间
            'updateTicketEffectTime' : function(){
                return updateTicketEffectTime1;
            },
            'getDate' : function(){
                return getDate1;
            },
            'str2date' : function(){
                return str2date1;
            },
            'getroyalocOrdersState' : function(){
                return getroyalocOrdersState1;
            }
          }
        });

        modalInstance.result.then(function () {
          //load();
        }, function () {
          //$log.info('Modal dismissed at: ' + new Date());
        });

    };

    //打开模态框
    $scope.orderinfo = function(obj){
     
        var modalInstance = $modal.open({
          template: require('../views/orderinfo.html'),
          controller: 'orderinfo11',
          size: 'lg',
          resolve: {
            'obj' : function(){
                return obj;
            }
            // device_code : function(){
            //     return device_code;
            // },
            // typelist : function(){
            //     return typelist;
            // },
            // add : function(){
            //     return add;
            // },
            // del : function(){
            //     return del;
            // }
          }
        });

        modalInstance.result.then(function () {
          //load();
        }, function () {
          //$log.info('Modal dismissed at: ' + new Date());
        });
    }


    $scope.resend = function(obj){
        var fun;
       

        var para = {};
        if(obj.sale_belong === 'juyou' || obj.sale_belong.indexOf('supply_piaofutong') == 0 ||  obj.sale_belong.indexOf('supply_tstc') == 0 ||  obj.sale_belong === 'supply_tongchenglvyou' || obj.sale_belong === 'supply_zhiyoubao')
        {
            fun = resend;
            para['code'] = obj.code;
        }
        else if(obj.sale_belong == 'langdao')
        {
            fun = getRedCorridorResentMsg;
            para['code'] = obj.code;
        }
        else if(obj.sale_belong == 'huaxiapiaolian')
        {
            fun = agencyOrderRepeatECode;
            para['order_code'] = obj.code;
            para['ownerMobile'] = obj.mobile;
        }

        fun.save(para, function(res){
            if(res.errcode === 0)
            {
                alert('发送成功');
            }
            else
            {
                alert(res.errmsg);
            }
        });
    }

    $scope.relay = function(obj){

        var relay1 = relay;
        var getRedCorridorTrSendSms1 = getRedCorridorTrSendSms;
        var agencyOrderRepeatECode1 = agencyOrderRepeatECode;
        
        var modalInstance = $modal.open({
          template: require('../views/relaymessage.html'),
          controller: 'relaymessage',
          size: 'xs',
          resolve: {
            code : function(){
                return obj.code;
            },
            'relay' : function(){
                return relay1;
            },
            'getRedCorridorTrSendSms' : function(){
                return getRedCorridorTrSendSms1;
            },
            'sale_belong' : function(){
                return obj.sale_belong;
            },
            'agencyOrderRepeatECode' : function(){
                return agencyOrderRepeatECode1;
            }
          }
        });

        modalInstance.result.then(function () {
            $scope.load();
        }, function () {
            
        });
    }

    $scope.back = function(obj, flag){

        var createBackOrder1 = createBackOrder;

        var modalInstance = $modal.open({
          template: require('../views/backticket.html'),
          controller: 'backticket',
          size: 'xs',
          resolve: {
            code : function(){
                return obj.code;
            },
            num : function(){
                return obj.num;
            },
            'createBackOrder' : function(){
                return createBackOrder1;
            },
            flag : function(){
                return flag;
            },
            from_app_id : function(){
                return obj.from_app_id;
            },
            subsidymoney : function(){
                return obj.business_order_actual_subsidy;
            },
          }
        });

        modalInstance.result.then(function () {
            $scope.load();
        }, function () {
            
        });
    }

    $scope.testBack = function(obj){

        var createBackOrder1 = createBackOrder;
        
        var modalInstance = $modal.open({
          template: require('../views/backticket.html'),
          controller: 'backticket',
          size: 'xs',
          resolve: {
            code : function(){
                return obj.code;
            },
            num : function(){
                return obj.num;
            },
            'createBackOrder' : function(){
                return testCreateBackOrder1;
            }
          }
        });

        modalInstance.result.then(function () {
            $scope.load();
        }, function () {
            
        });
    }

    //个人信息
    $scope.userMsg = function(obj){
        var modalInstance = $modal.open({
            template: require('../views/userMsg.html'),
            controller: 'userMsg',
            size: 'lg',
            resolve: {
                uid : function(){
                    return obj.uid;
                }
            }
        });

        modalInstance.result.then(function () {

        });
    }

    //商客订单查看分润信息
    $scope.skv4Profit = function(obj){
        var para = {
            'order_code': obj.code
        }
        var modalInstance = $modal.open({
			template: require('../../55skwx/views/skwxShopOrderProfit.html'),
			controller: 'skwxShopOrderProfit',
			size: 'lg',
			resolve: {
				bean: function () {
					return para;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			// $scope.loadlist();
		});
    }



    $scope.dateOpen = function($event, item){
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };


    //----以下是页面支付时间快捷选择日期按钮-----
    $scope.today = function(){
        var now = new Date();
        $scope.date.lable = getDate(now);
        $scope.date1.lable = $scope.date.lable;
    }

    $scope.yesterday = function(){
        var now = new Date();
        now.setDate(now.getDate() - 1);
        $scope.date.lable = getDate(now);
        $scope.date1.lable = getDate(now);
    }

    $scope.thisWeek = function(){
        var now = new Date();
        $scope.date1.lable = getDate(now);

        var week = now.getDay() - 1;
        week = week == -1 ? 6 : week
        now.setDate(now.getDate() - week);
        $scope.date.lable = getDate(now);
    }

    $scope.lastWeek = function(){
        var now = new Date();
        var week = now.getDay() - 1;
        week = week == -1 ? 6 : week
        now.setDate(now.getDate() - week);
        $scope.date1.lable = getDate(now);

        now.setDate(now.getDate() - 7);
        $scope.date.lable = getDate(now);
    }

    $scope.thisMonth = function(){
        var now = new Date();
        $scope.date1.lable = getDate(now);
        
        now.setDate(1);
        $scope.date.lable = getDate(now);
    }

    $scope.lastMonth = function(){
        var now = new Date();
        now.setDate(0);
        $scope.date1.lable = getDate(now);

        now.setDate(1);
        $scope.date.lable = getDate(now);
    }
    //----以下是页面购买时间快捷选择日期按钮-----
    $scope.todayPay = function(){
        var now = new Date();
        $scope.date2.lable = getDate(now);
        $scope.date3.lable = $scope.date2.lable;
    }

    $scope.yesterdayPay = function(){
        var now = new Date();
        now.setDate(now.getDate() - 1);
        $scope.date2.lable = getDate(now);
        $scope.date3.lable = getDate(now);
    }

    $scope.thisWeekPay = function(){
        var now = new Date();
        $scope.date3.lable = getDate(now);

        var week = now.getDay() - 1;
        week = week == -1 ? 6 : week
        now.setDate(now.getDate() - week);
        $scope.date2.lable = getDate(now);
    }

    $scope.lastWeekPay = function(){
        var now = new Date();
        var week = now.getDay() - 1;
        week = week == -1 ? 6 : week
        now.setDate(now.getDate() - week);
        $scope.date3.lable = getDate(now);

        now.setDate(now.getDate() - 7);
        $scope.date2.lable = getDate(now);
    }

    $scope.thisMonthPay = function(){
        var now = new Date();
        $scope.date3.lable = getDate(now);
        
        now.setDate(1);
        $scope.date2.lable = getDate(now);
    }

    $scope.lastMonthPay = function(){
        var now = new Date();
        now.setDate(0);
        $scope.date3.lable = getDate(now);

        now.setDate(1);
        $scope.date2.lable = getDate(now);
    }

    //打开详情模态框
    $scope.cardInfo = function(obj){
      console.log(obj, '!!!!!!')
      var obj1 = obj
      var list1 = list
      var findByorderCartCode1 = findByorderCartCode
      var modalInstance = $modal.open({
        template: require('../views/ordercarinfo.html'),
        controller: 'ordercarinfo',
        size: 'lg',
        resolve: {
          'obj' : function(){
              return obj1;
          },
          'list' : function(){
              return list1;
          },
          'findByorderCartCode' : function(){
              return findByorderCartCode1;
          }
          // add : function(){
          //     return add;
          // },
          // del : function(){
          //     return del;
          // }
        }
      });

      modalInstance.result.then(function () {
        //load();
      }, function () {
        //$log.info('Modal dismissed at: ' + new Date());
      });
  }


};